package com.danco.training.controller;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Console;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.danco.training.bookshop.entity.storage.Bookshop;
import com.danco.training.serialization.FileLoaderUtil;
import com.danco.training.serialization.FileSaverUtil;
import com.danco.training.view.Creator;
import com.danco.training.view.IOUtil;
import com.danco.training.view.Navigator;

public class MenuController implements Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4338319931051486591L;

	public void runMenu() throws IOException, ClassNotFoundException {

		
		Creator c = new Creator();
		c.createMenu();
		Navigator n = new Navigator();
		n.setCurrentMenu(c.getMainMenu());
		n.printMenu();
		int index = IOUtil.readInt();
		if(n.getCurrentMenu().getMenuItemByKey(index).getMenu() == null){
			IOUtil.write("Bye");
			
		}
		
		while (n.getCurrentMenu().getMenuItemByKey(index).getMenu() != null){
			
			n.getCurrentMenu().getMenuItemByKey(index).doAction();
			n.navigate(index);
			n.printMenu();
			index = IOUtil.readInt();
			if(n.getCurrentMenu().getMenuItemByKey(index).getMenu() == null){
				IOUtil.write("Bye");
				FileSaverUtil.save(Bookshop.getBookshopInstance());
			}
			
			
			} 
			
		
		
		
		
		
		
		
		
		

	}

}
